package com.fau.cm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalKeyValue;
import com.fau.cm.entity.MedicalCallSMSPhrase;
import com.fau.cm.entity.MedicalCallSms;
import com.fau.cm.entity.MedicalGroup;
import com.fau.cm.entity.MedicalPlatform;
import com.fau.cm.service.ISmsManagerService;
import com.fau.cm.util.Page;
import com.fau.cm.util.ParamUtil;
import com.fau.cm.util.SystemConfig;
import com.fau.cm.util.ValidatorUtil;

@Service
@Transactional(readOnly = true)
public class SmsManagerService extends BaseService implements ISmsManagerService{

	@Override
	public Page findSmsByPage(MedicalAdmin admin, MedicalCallSms callSms, Map<String, Object> map) {
		StringBuilder statusSql = new StringBuilder("from MedicalCallSms a where 1=1 and a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallSms a where 1=1 and a.hospitalId=? ");
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getEnterpriseId());
		if(StringUtils.isNotBlank(callSms.getPhoneNum())){
			countSql.append("and a.phoneNum = ? ");
			statusSql.append("and a.phoneNum = ? ");
			objectList.add(callSms.getPhoneNum());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendStatus())){
			countSql.append("and a.sendStatus = ? ");
			statusSql.append("and a.sendStatus = ? ");
			objectList.add(callSms.getSendStatus());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendSeat())){
			countSql.append("and a.sendSeat = ? ");
			statusSql.append("and a.sendSeat = ? ");
			objectList.add(callSms.getSendSeat());
		}
		if(StringUtils.isNotBlank(callSms.getCenterId())){
			countSql.append("and a.centerId = ? ");
			statusSql.append("and a.centerId = ? ");
			objectList.add(callSms.getCenterId());
		}
		if(StringUtils.isNotBlank(callSms.getGroupId())){
			countSql.append("and a.groupId = ? ");
			statusSql.append("and a.groupId = ? ");
			objectList.add(callSms.getGroupId());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallSms> callSmsList = new ArrayList<MedicalCallSms>();
		for(Object obj : page.getResult()){
			callSms = (MedicalCallSms)obj;
			MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
			callSms.setStatusVal(keyVal.getKeyValue());
			MedicalAdmin sendSeat = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
			callSms.setSendSeatName(sendSeat.getName());
			callSmsList.add(callSms);
		}
		page.setResult(callSmsList);
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveSms(MedicalAdmin admin, MedicalCallSms callSms) {
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(callSms);
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			callSms.setCenterId(admin.getGroup().getPlatform().getId());
		}
		callSms.setHospitalId(admin.getEnterpriseId());
		callSms.setSendStatus(SystemConfig.SMS_NO_SEND_TYPE);
		callSms.setSendSeat(admin.getId());
		callSms.setSendSeatName(admin.getName());
		if(errorInfo.size()>0){
			return false;
		}else{
			return this.iBaseDao.saveObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delSms(MedicalCallSms callSms) {
		if(callSms.getId().indexOf(",") != -1){
			String[] ids = callSms.getId().split(",");
			for(String id : ids){
				this.iBaseDao.updateHql("delete from MedicalCallSms a where a.id=? ", id);
			}
		}else{
			this.iBaseDao.deleteObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveEditCallSms(MedicalCallSms callSms) {
		MedicalCallSms bean = (MedicalCallSms) this.iBaseDao.getObject(MedicalCallSms.class, callSms.getId());
		bean.setPhoneNum(callSms.getPhoneNum());
		bean.setContent(callSms.getContent());
		bean.setUpdateTime(new Date());
		this.iBaseDao.saveObject(bean);
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelAllSms(MedicalAdmin admin) throws Exception {
		String realPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(realPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		CellStyle style = workbook.createCellStyle();
		style.setWrapText(true);
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallSms> listcallSms = this.iBaseDao.getBySql("from MedicalCallSms a where a.hospitalId=? ", null, admin.getEnterpriseId());
			for(int i=0;i<listcallSms.size();i++){
				Row currentRow = sheet.createRow(i+1);
				MedicalCallSms callSms = listcallSms.get(i);
				currentRow.createCell(0).setCellValue(callSms.getPhoneNum());
				currentRow.createCell(1).setCellValue(callSms.getContent());
				if(null != callSms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(callSms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != callSms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(callSms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(StringUtils.isNotBlank(callSms.getSendSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != callSms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
				
			}
		return workbook;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelCurrentSms(MedicalAdmin admin, String ids) throws Exception {
		
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] callSmsId =  ids.split(",");
			for(int j=0;j<callSmsId .length;j++){
				// 获得记录
				MedicalCallSms callSms = (MedicalCallSms)this.iBaseDao.getObject(MedicalCallSms.class, callSmsId[j]);
				Row currentRow = sheet.createRow(j+1);
				currentRow.createCell(0).setCellValue(callSms.getPhoneNum());
				currentRow.createCell(1).setCellValue(callSms.getContent());
				if(null != callSms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(callSms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != callSms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(callSms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(StringUtils.isNotBlank(callSms.getSendSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != callSms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
			
			}
	}
		return workbook;
			
	}	

	@Override
	public Page findSmsSettingsByPage(MedicalAdmin admin, MedicalCallSMSPhrase phrase, Map<String, Object> map) {
		List<Object> objectList = new ArrayList<Object>();
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallSMSPhrase a where a.hospitalId=? ");
		StringBuilder statusSql = new StringBuilder("from MedicalCallSMSPhrase a where a.hospitalId=? ");
		
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(phrase.getType())){
			countSql.append("and a.type=? ");
			statusSql.append("and a.type=? ");
			objectList.add(phrase.getType());
		}
		if(null != phrase.getStime()){
			countSql.append("and a.createTime>=? ");
			statusSql.append("and a.createTime>=? ");
			objectList.add(phrase.getStime());
		}
		if(null != phrase.getEtime()){
			countSql.append("and a.createTime<=? ");
			statusSql.append("and a.createTime<=? ");
			objectList.add(phrase.getEtime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallSMSPhrase> list = new ArrayList<MedicalCallSMSPhrase>();
		for(Object obj : page.getResult()){
			phrase = (MedicalCallSMSPhrase)obj;
			MedicalKeyValue  keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), phrase.getType());
			phrase.setKeyVal(keyVal.getKeyValue());
			list.add(phrase);
		}
		page.setResult(list);
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editSave(MedicalCallSMSPhrase phrase) {
		MedicalCallSMSPhrase p = (MedicalCallSMSPhrase) this.iBaseDao.getObject(MedicalCallSMSPhrase.class, phrase.getId());
		p.setContent(phrase.getContent());
		p.setType(phrase.getType());
		p.setUpdateTime(phrase.getUpdateTime());
		this.iBaseDao.saveObject(p);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveAdd(MedicalAdmin admin, MedicalCallSMSPhrase phrase) {
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			phrase.setGroupId(admin.getGroup().getId());
			phrase.setCenterId(admin.getGroup().getPlatform().getId());
		}
		phrase.setHospitalId(admin.getEnterpriseId());
		return this.iBaseDao.saveObject(phrase);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deletePhrase(String ids) {
		String [] ordersIds =  ids.split(",");
		for(int i=0;i<ordersIds.length;i++){
			MedicalCallSMSPhrase  phrase = (MedicalCallSMSPhrase) this.iBaseDao.getObject(MedicalCallSMSPhrase.class, ordersIds[i]);
			this.iBaseDao.deleteObject(phrase);
		}
	}

	@Override
	public Page findSmsCenterByPage(MedicalAdmin admin, MedicalCallSms callSms, Map<String, Object> map) {
		StringBuilder statusSql = new StringBuilder("from MedicalCallSms a where a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallSms a where a.hospitalId=? ");
		List<Object> objectList = new ArrayList<Object>();
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.centerId=? ");
			countSql.append("and a.centerId=? ");
			objectList.add(admin.getGroup().getPlatform().getId());
		}
		objectList.add(admin.getEnterpriseId());
		if(StringUtils.isNotBlank(callSms.getPhoneNum())){
			countSql.append("and a.phoneNum = ? ");
			statusSql.append("and a.phoneNum = ? ");
			objectList.add(callSms.getPhoneNum());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendStatus())){
			countSql.append("and a.sendStatus = ? ");
			statusSql.append("and a.sendStatus = ? ");
			objectList.add(callSms.getSendStatus());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendSeat())){
			countSql.append("and a.sendSeat = ? ");
			statusSql.append("and a.sendSeat = ? ");
			objectList.add(callSms.getSendSeat());
		}
		if(StringUtils.isNotBlank(callSms.getGroupId())){
			countSql.append("and a.groupId = ? ");
			statusSql.append("and a.groupId = ? ");
			objectList.add(callSms.getGroupId());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallSms> callSmsList = new ArrayList<MedicalCallSms>();
		for(Object obj : page.getResult()){
			callSms = (MedicalCallSms)obj;
			MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
			callSms.setStatusVal(keyVal.getKeyValue());
			MedicalAdmin sendSeat = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
			callSms.setSendSeatName(sendSeat.getName());
			callSmsList.add(callSms);
		}
		page.setResult(callSmsList);
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveCenterSms(MedicalAdmin admin, MedicalCallSms callSms) {
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(callSms);
		if(errorInfo.size()>0){
			return false;
		}else{
			if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
				callSms.setGroupId(admin.getGroup().getId());
				callSms.setCenterId(admin.getGroup().getPlatform().getId());
			}
			callSms.setHospitalId(admin.getEnterpriseId());
			return this.iBaseDao.saveObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delCenterSms(MedicalCallSms callSms) {
		if(callSms.getId().indexOf(",") != -1){
			String[] ids = callSms.getId().split(",");
			for(String id : ids){
				this.iBaseDao.updateHql("delete from MedicalCallSms a where a.id=? ", id);
			}
		}else{
			this.iBaseDao.deleteObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveEditCenterCallSms(MedicalCallSms callSms) {
		MedicalCallSms bean = (MedicalCallSms) this.iBaseDao.getObject(MedicalCallSms.class, callSms.getId());
		bean.setPhoneNum(callSms.getPhoneNum());
		bean.setContent(callSms.getContent());
		bean.setCreateTime(callSms.getCreateTime());
		bean.setSendTime(callSms.getSendTime());
		bean.setSendSeat(callSms.getSendSeat());
		bean.setSendStatus(callSms.getSendStatus());
		bean.setUpdateTime(new Date());
		this.iBaseDao.saveObject(bean);
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelAllCenterSms(MedicalAdmin admin,MedicalCallSms callSms) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallSms a where ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("a.centerId=?");
			objectList.add(admin.getGroup().getPlatform().getId());
		} else {
			statusSql.append("a.hospitalId=?");
			objectList.add(admin.getEnterpriseId());
		}
		
		if(StringUtils.isNotBlank(callSms.getPhoneNum())){
			statusSql.append("and a.phoneNum = ? ");
			objectList.add(callSms.getPhoneNum());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendStatus())){
			statusSql.append("and a.sendStatus = ? ");
			objectList.add(callSms.getSendStatus());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendSeat())){
			statusSql.append("and a.sendSeat = ? ");
			objectList.add(callSms.getSendSeat());
		}
		if(StringUtils.isNotBlank(callSms.getGroupId())){
			statusSql.append("and a.groupId = ? ");
			objectList.add(callSms.getGroupId());
		}
		
		String realPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(realPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallSms> listcallSms = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
			for(int i=0;i<listcallSms.size();i++){
				Row currentRow = sheet.createRow(i+1);
				MedicalCallSms sms = listcallSms.get(i);
				currentRow.createCell(0).setCellValue(sms.getPhoneNum());
				currentRow.createCell(1).setCellValue(sms.getContent());
				if(null != sms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(sms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != sms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(sms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(StringUtils.isNotBlank(sms.getSendSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, sms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != sms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), sms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
				
			}
		return workbook;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelCurrentCenterSms(MedicalAdmin admin, String ids) throws Exception {
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] callSmsId =  ids.split(",");
			for(int j=0;j<callSmsId .length;j++){
				// 获得记录
				MedicalCallSms callSms = (MedicalCallSms)this.iBaseDao.getObject(MedicalCallSms.class, callSmsId[j]);
				Row currentRow = sheet.createRow(j+1);
				
				currentRow.createCell(0).setCellValue(callSms.getPhoneNum());
				currentRow.createCell(1).setCellValue(callSms.getContent());
				
				if(null != callSms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(callSms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != callSms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(callSms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(null != callSms.getSendSeat()){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != callSms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
			
			}
	}
		return workbook;		
	}	
	
	@Override
	public Page findSmsGroupByPage(MedicalAdmin admin, MedicalCallSms callSms, Map<String, Object> map) {
		StringBuilder statusSql = new StringBuilder("from MedicalCallSms a where a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallSms a where a.hospitalId=? ");
		List<Object> objectList = new ArrayList<Object>();
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.groupId=? ");
			countSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(callSms.getPhoneNum())){
			countSql.append("and a.phoneNum = ? ");
			statusSql.append("and a.phoneNum = ? ");
			objectList.add(callSms.getPhoneNum());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendStatus())){
			countSql.append("and a.sendStatus = ? ");
			statusSql.append("and a.sendStatus = ? ");
			objectList.add(callSms.getSendStatus());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendSeat())){
			countSql.append("and a.sendSeat = ? ");
			statusSql.append("and a.sendSeat = ? ");
			objectList.add(callSms.getSendSeat());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallSms> callSmsList = new ArrayList<MedicalCallSms>();
		for(Object obj : page.getResult()){
			callSms = (MedicalCallSms)obj;
			MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
			callSms.setStatusVal(keyVal.getKeyValue());
			MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
			callSms.setSendSeatName(medicalAdmin.getName());
			callSmsList.add(callSms);
			
		}
		page.setResult(callSmsList);
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveGroupSms(MedicalAdmin admin, MedicalCallSms callSms) {
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(callSms);
		if(errorInfo.size()>0){
			return false;
		}else{
			if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
				callSms.setGroupId(admin.getGroup().getId());
			}
			callSms.setHospitalId(admin.getEnterpriseId());
			return this.iBaseDao.saveObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delGroupSms(MedicalCallSms callSms) {
		if(callSms.getId().indexOf(",") != -1){
			String[] ids = callSms.getId().split(",");
			for(String id : ids){
				this.iBaseDao.updateHql("delete from MedicalCallSms a where a.id=? ", id);
			}
		}else{
			this.iBaseDao.deleteObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveEditGroupCallSms(MedicalCallSms callSms) {
		MedicalCallSms bean = (MedicalCallSms) this.iBaseDao.getObject(MedicalCallSms.class, callSms.getId());
		bean.setPhoneNum(callSms.getPhoneNum());
		bean.setContent(callSms.getContent());
		bean.setCreateTime(callSms.getCreateTime());
		bean.setSendTime(callSms.getSendTime());
		bean.setSendSeat(callSms.getSendSeat());
		bean.setSendStatus(callSms.getSendStatus());
		bean.setUpdateTime(new Date());
		this.iBaseDao.saveObject(bean);
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelAllGroupSms(MedicalAdmin admin,MedicalCallSms callSms) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallSms a where a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.groupId=? ");
			objectList.add(admin.getGroup().getId());
		}
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(callSms.getPhoneNum())){
			statusSql.append("and a.phoneNum = ? ");
			objectList.add(callSms.getPhoneNum());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendStatus())){
			statusSql.append("and a.sendStatus = ? ");
			objectList.add(callSms.getSendStatus());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendSeat())){
			statusSql.append("and a.sendSeat = ? ");
			objectList.add(callSms.getSendSeat());
		}
		
		String realPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(realPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		
		List<MedicalCallSms> listcallSms = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		
			for(int i=0;i<listcallSms.size();i++){
				Row currentRow = sheet.createRow(i+1);
				MedicalCallSms sms = listcallSms.get(i);
				currentRow.createCell(0).setCellValue(sms.getPhoneNum());
				currentRow.createCell(1).setCellValue(sms.getContent());
				if(null != sms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(sms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != sms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(sms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(StringUtils.isNotBlank(sms.getSendSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, sms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != sms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), sms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
			}
		return workbook;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelCurrentGroupSms(MedicalAdmin admin, String ids) throws Exception {
		
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] callSmsId =  ids.split(",");
			for(int j=0;j<callSmsId .length;j++){
				// 获得记录
				MedicalCallSms callSms = (MedicalCallSms)this.iBaseDao.getObject(MedicalCallSms.class, callSmsId[j]);
				Row currentRow = sheet.createRow(j+1);
				
				currentRow.createCell(0).setCellValue(callSms.getPhoneNum());
				currentRow.createCell(1).setCellValue(callSms.getContent());
				
				if(null != callSms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(callSms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != callSms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(callSms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(null != callSms.getSendSeat()){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != callSms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
			
			}
	}
		return workbook;		
	}
	
	@Override
	public Page findSmsMyByPage(MedicalAdmin admin, MedicalCallSms callSms, Map<String, Object> map) {
		StringBuilder statusSql = new StringBuilder("from MedicalCallSms a where a.sendSeat=? and a.hospitalId=? ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallSms a where a.sendSeat=? and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getId());
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(callSms.getPhoneNum())){
			countSql.append("and a.phoneNum = ? ");
			statusSql.append("and a.phoneNum = ? ");
			objectList.add(callSms.getPhoneNum());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendStatus())){
			countSql.append("and a.sendStatus = ? ");
			statusSql.append("and a.sendStatus = ? ");
			objectList.add(callSms.getSendStatus());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20),
				map, countSql.toString(), statusSql.toString(), objectList);
		
		List<MedicalCallSms> callSmsList = new ArrayList<MedicalCallSms>();
		for(Object obj : page.getResult()){
			callSms = (MedicalCallSms)obj;
			MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
			callSms.setStatusVal(keyVal.getKeyValue());
			MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class,callSms.getSendSeat());
			callSms.setSendSeatName(medicalAdmin.getName());
			callSmsList.add(callSms);
		}
		page.setResult(callSmsList);
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveMySms(MedicalAdmin admin, MedicalCallSms callSms) {
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(callSms);
		if(errorInfo.size()>0){
			return false;
		}else{
			callSms.setSendSeat(admin.getId());
			callSms.setHospitalId(admin.getEnterpriseId());
			return this.iBaseDao.saveObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delMySms(MedicalCallSms callSms) {
		if(callSms.getId().indexOf(",") != -1){
			String[] ids = callSms.getId().split(",");
			for(String id : ids){
				this.iBaseDao.updateHql("delete from MedicalCallSms a where a.id=? ", id);
			}
		}else{
			this.iBaseDao.deleteObject(callSms);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveEditMyCallSms(MedicalCallSms callSms) {
		MedicalCallSms bean = (MedicalCallSms) this.iBaseDao.getObject(MedicalCallSms.class, callSms.getId());
		bean.setPhoneNum(callSms.getPhoneNum());
		bean.setContent(callSms.getContent());
		bean.setCreateTime(callSms.getCreateTime());
		bean.setSendTime(callSms.getSendTime());
		bean.setSendSeat(callSms.getSendSeat());
		bean.setSendStatus(callSms.getSendStatus());
		bean.setUpdateTime(new Date());
		this.iBaseDao.saveObject(bean);
	}

	@SuppressWarnings({ "unchecked" })
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelAllMySms(MedicalAdmin admin,MedicalCallSms callSms) throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallSms a where a.sendSeat=? and a.hospitalId=? ");
		
		List<Object> objectList = new ArrayList<Object>();
		objectList.add(admin.getId());
		objectList.add(admin.getEnterpriseId());
		
		if(StringUtils.isNotBlank(callSms.getPhoneNum())){
			statusSql.append("and a.phoneNum = ? ");
			objectList.add(callSms.getPhoneNum());
		}
		
		if(StringUtils.isNotBlank(callSms.getSendStatus())){
			statusSql.append("and a.sendStatus = ? ");
			objectList.add(callSms.getSendStatus());
		}
		
		String realPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(realPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		
		Sheet sheet = workbook.getSheetAt(0);
		List<MedicalCallSms> listcallSms = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
			for(int i=0;i<listcallSms.size();i++){
				Row currentRow = sheet.createRow(i+1);
				MedicalCallSms sms = listcallSms.get(i);
				currentRow.createCell(0).setCellValue(sms.getPhoneNum());
				currentRow.createCell(1).setCellValue(sms.getContent());
				if(null != sms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(sms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != sms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(sms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(StringUtils.isNotBlank(sms.getSendSeat())){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, sms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != sms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), sms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
				
			}
		return workbook;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Workbook exportExcelCurrentMySms(MedicalAdmin admin, String ids) throws Exception {
		
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_SMS);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		if(StringUtils.isNotBlank(ids)){
			String [] callSmsId =  ids.split(",");
			for(int j=0;j<callSmsId .length;j++){
				MedicalCallSms callSms = (MedicalCallSms)this.iBaseDao.getObject(MedicalCallSms.class, callSmsId[j]);
				Row currentRow = sheet.createRow(j+1);
				
				currentRow.createCell(0).setCellValue(callSms.getPhoneNum());
				currentRow.createCell(1).setCellValue(callSms.getContent());
				
				if(null != callSms.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(callSms.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				if(null != callSms.getSendTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(callSms.getSendTime(),"yyyy-MM-dd HH:mm:ss"));
				}
				
				if(null != callSms.getSendSeat()){
					MedicalAdmin medicalAdmin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
					currentRow.createCell(4).setCellValue(medicalAdmin.getName());
				}
				if(null != callSms.getSendStatus()){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
					currentRow.createCell(5).setCellValue(keyVal.getKeyValue());
				}
			
			}
	}
		return workbook;		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalCallSms> findCustomerSmsList(String cId) {
		List<MedicalCallSms> list = this.iBaseDao.getBySql("from MedicalCallSms a where a.customerId=? ", null, cId);
		List<MedicalCallSms> list1 = new ArrayList<MedicalCallSms>();
		for(MedicalCallSms callSms : list) {
			MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, callSms.getSendSeat());
			callSms.setSendSeatName(admin.getUsername());
			MedicalKeyValue kv = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), callSms.getSendStatus());
			callSms.setStatusVal(kv.getKeyValue());
			list1.add(callSms);
		}
		return list1;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalGroup> findAllCurSerGroup(String cid) {
		List<MedicalGroup> list = new ArrayList<MedicalGroup>();
		if(StringUtils.isNotBlank(cid)) {
			list = this.iBaseDao.getBySql("from MedicalGroup a where a.platform.id=? ", null, cid);
		}else {
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
				list = this.iBaseDao.getBySql("from MedicalGroup a where a.platform.id=? ", null, admin.getGroup().getPlatform().getId());
			} else {
				list = this.iBaseDao.getBySql("from MedicalGroup a where a.eid=? and a.platform.platformType=? ", null, admin.getEnterpriseId(), SystemConfig.CALL_CENTER_TYPE);
			}
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalAdmin> findCurSerByGroupId(String groupId) {
		List<MedicalAdmin> list = new ArrayList<MedicalAdmin>();
		if(StringUtils.isBlank(groupId)){
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
				list = this.iBaseDao.getBySql("from MedicalAdmin a where a.group.id=? ", null, admin.getGroup().getId());
			} else {
				List<MedicalGroup> groupList = this.iBaseDao.getBySql("from MedicalGroup a where a.eid=? and a.platform.platformType=? ", null, admin.getEnterpriseId(), SystemConfig.CALL_CENTER_TYPE);
				for(MedicalGroup group : groupList) {
					List<MedicalAdmin> list0 = this.iBaseDao.getBySql("from MedicalAdmin a where a.group.id=? ", null, group.getId());
					list.addAll(list0);
				}
			}
		}else{
			list = this.iBaseDao.getBySql("from MedicalAdmin a where a.group.id=? ", null, groupId);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalPlatform> findAllCurSerCenter(MedicalAdmin admin) {
		List<MedicalPlatform> list = this.iBaseDao.getBySql("from MedicalPlatform a where a.eid=? and a.platformType=? ", null, admin.getEnterpriseId(),SystemConfig.CALL_CENTER_TYPE);
		return list;
	}

	@Override
	public List<MedicalCallSMSPhrase> findSmsPhraseByType(String type) {
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		@SuppressWarnings("unchecked")
		List<MedicalCallSMSPhrase> list = this.iBaseDao.getBySql("from MedicalCallSMSPhrase a where a.hospitalId=? and a.type=? ", null,admin.getEnterpriseId(), type);
		return list;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String importSmsExcel(MedicalAdmin admin, File file)
			throws Exception {
		
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		
		Sheet sheet = workbook.getSheetAt(0);
		for (Row row : sheet) {
			if(row.getRowNum() == 0){
				continue;
			}else{
				MedicalCallSms sms = new MedicalCallSms();
				if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
					sms.setCenterId(admin.getGroup().getPlatform().getId());
				}
				sms.setHospitalId(admin.getEnterpriseId());
				sms.setSendStatus(SystemConfig.SMS_NO_SEND_TYPE);
				sms.setSendSeat(admin.getId());
				sms.setSendSeatName(admin.getName());
				row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
				String phone = row.getCell(0).getStringCellValue();
				if(StringUtils.isNotBlank(phone)) {
					sms.setPhoneNum(phone);
				}else{
					return "第"+(row.getRowNum()+1)+"行为空";
				}
				if(StringUtils.isNotBlank(row.getCell(1).getStringCellValue().trim())){
					sms.setContent(row.getCell(1).getStringCellValue().trim());
				}else{
					return "第"+(row.getRowNum()+1)+"行为空";
				}
				this.iBaseDao.saveObject(sms);
			}
		}
		return "true";
	}

	@Override
	public boolean isSend() {
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		MedicalKeyValue keyValue = (MedicalKeyValue) this.iBaseDao.getObjectByHql("from MedicalKeyValue a where a.hospitalId=? and a.systemType=? and a.typeName=? and a.keyName=? ",
				admin.getEnterpriseId(),
				SystemConfig.CM_SYSTEM_TYPE,
				SystemConfig.SMS_RM,
				SystemConfig.SMS_BIRTHDAY);
		// 1发送生日短信
		if(keyValue.getKeyValue().equals("1")){
			return true;
		}else{
			return false;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean saveSwitch(String flag) {
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		MedicalKeyValue keyValue = (MedicalKeyValue) this.iBaseDao.getObjectByHql("from MedicalKeyValue a where a.hospitalId=? and a.systemType=? and a.typeName=? and a.keyName=? ",
				admin.getEnterpriseId(),
				SystemConfig.CM_SYSTEM_TYPE,
				SystemConfig.SMS_RM,
				SystemConfig.SMS_BIRTHDAY);
		if(Boolean.valueOf(flag)){
			keyValue.setKeyValue("1");
		}else{
			keyValue.setKeyValue("0");
		}
		return true;
	}
	
}
